﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using IEQ_Superintendencia.Domain.Control;
using IEQ_Superintendencia.Domain.Entity;

namespace IEQ_Superintendencia.User_Interface
{
    public partial class UICadIgrejaPast : UserControl
    {
        int _pastor_id;
        int _igreja_id;


        public UICadIgrejaPast()
        {
            InitializeComponent();
            plnPrincipal.Anchor = AnchorStyles.None;
            //nvgCadIgrejaPastoreada.Enabled = false;

            preencheCbPastorIgreja();
            preencheCbPastor();
            //IEQ_Superintendencia.Services.Persistence.dsIEQSuper.IGREJA_PASTOREADADataTable dtIgPast = new IEQ_Superintendencia.Services.Persistence.dsIEQSuper.IGREJA_PASTOREADADataTable();
            //IEQ_Superintendencia.Services.Persistence.dsIEQSuperTableAdapters.IGREJA_PASTOREADATableAdapter taIgPast = new IEQ_Superintendencia.Services.Persistence.dsIEQSuperTableAdapters.IGREJA_PASTOREADATableAdapter();
            //taIgPast.Fill(dtIgPast);
            //gvIgrejasPastoreadas.DataSource = dtIgPast;
            

       }

        public void cadIgrejaPast(int _pastor_id, int _igreja_id)
        {
            this._pastor_id = _pastor_id;
            this._igreja_id = _igreja_id;

            nvgCadIgrejaPastoreada.Enabled = true;
            
            cbIgreja.Text = _igreja_id.ToString();
            tbPastorID.Text = _pastor_id.ToString();
            tbPastorID.Enabled = false;
            
        }

        protected void FactoryIgrejaPastoreada(ref IgrejaPastoreada _igrejaPastoreada)
        {            
            _igrejaPastoreada.igreja_id = Int32.Parse(tbPastorID.Text);
            _igrejaPastoreada.igreja_pastoreada_data_entrada = DateTime.Parse(mtbDtEntrada.Text);
            _igrejaPastoreada.igreja_pastoreada_data_saida = DateTime.Parse(mtbDtEntrada.Text);
            _igrejaPastoreada.igreja_pastoreada_pastor_anterior = Int32.Parse(cbPastorAnterior.SelectedValue.ToString());
            _igrejaPastoreada.igreja_pastoreada_regiao = Int32.Parse(tbRegiao.Value.ToString());
            _igrejaPastoreada.igreja_pastoreada_superintendente = tbSuperintendente.Text;
            _igrejaPastoreada.igreja_pastoreada_tel_superintendencia = mtbTelSuperint.Text;            
            _igrejaPastoreada.igreja_pastoreada_observacao = tbObservacao.Text;
        }

        protected bool ValidaCadastroIgrejaPastoreada()
        {
            DateTime dtValida = new DateTime();

            if (IEQUtils.ParseData(mtbDtEntrada.Text, ref dtValida, "Data de entrada"))
                mtbDtEntrada.Text = dtValida.ToShortDateString();
            else
                return false;

            if (IEQUtils.ParseData(mtbDtSaida.Text, ref dtValida, "Data de saída"))
                mtbDtSaida.Text = dtValida.ToShortDateString();
            else
                return false;

            return true;
        }

        protected void preencheCbPastorIgreja()
        {
            IEQ_Superintendencia.Services.Persistence.dsIEQSuper.IGREJADataTable dtIgreja = new IEQ_Superintendencia.Services.Persistence.dsIEQSuper.IGREJADataTable();
            IEQ_Superintendencia.Services.Persistence.dsIEQSuperTableAdapters.IGREJATableAdapter dsIgreja = new IEQ_Superintendencia.Services.Persistence.dsIEQSuperTableAdapters.IGREJATableAdapter();

            dtIgreja = dsIgreja.GetData();
            cbIgreja.DataSource = dtIgreja;
            cbIgreja.DisplayMember = "IGREJA_NOME";
            cbIgreja.ValueMember = "IGREJA_ID"; 
            
        }

        protected void preencheCbPastor()
        {
            IEQ_Superintendencia.Services.Persistence.dsIEQSuper.PASTORDataTable dtPastor = new IEQ_Superintendencia.Services.Persistence.dsIEQSuper.PASTORDataTable();
            IEQ_Superintendencia.Services.Persistence.dsIEQSuperTableAdapters.PASTORTableAdapter dsPastor = new IEQ_Superintendencia.Services.Persistence.dsIEQSuperTableAdapters.PASTORTableAdapter();

            dtPastor = dsPastor.GetData();
            cbPastorAnterior.DataSource = dtPastor;
            cbPastorAnterior.DisplayMember = "PASTOR_NOME";
            cbPastorAnterior.ValueMember = "PASTOR_ID";

        }

        private void nvgAdicionar_Click(object sender, EventArgs e)
        {
            //tbPastorID.Text = _pastor_id.ToString();
            //cbIgreja.Text = _igreja_id.ToString();
            //tbPastorID.Enabled = false;
            //cbIgreja.Enabled = false;
        }

        private void nvgBtnSalvar(object sender, EventArgs e)
        {
            IgrejaPastoreada igrejaPast = new IgrejaPastoreada();
            if (ValidaCadastroIgrejaPastoreada())
            {
                FactoryIgrejaPastoreada(ref igrejaPast);

            

            #region Usando somente DS

            this.Validate();
            this.iGREJA_PASTOREADABindingSource.EndEdit();
            try
            {
                this.tableAdapterManager.UpdateAll(this.dsIEQSuper);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace.ToString());
            }
            #endregion
        }

        private void cbIgreja_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblIgrejaID.Text = cbIgreja.SelectedValue.ToString();
        }

        private void cbPastorAnterior_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblPastAnt.Text = cbPastorAnterior.SelectedValue.ToString();
        }

    }
}
